<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('名称')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goodsName">
              <a-input v-model="model.goodsName" :placeholder="$t('请输入名称')"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('总币值')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalCoin">
              <a-input-number v-model="model.totalCoin" :placeholder="$t('请输入总币值')" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('剩余币值')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remainCoin">
              <a-input-number v-model="model.remainCoin" :placeholder="$t('请输入剩余币值')" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('状态')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
              <j-dict-select-tag type="list" v-model="model.state" dictCode="bis_member_limit_coin.state" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('有效开始时间')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="validityStart">
              <j-date :placeholder="$t('请选择有效开始时间')" v-model="model.validityStart"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('有效结束时间')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="validityEnd">
              <j-date :placeholder="$t('请选择有效结束时间')" v-model="model.validityEnd"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('手机号')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="memberPhone">
              <a-input v-model="model.memberPhone" :placeholder="$t('请输入手机号')"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('卡号')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chipNo">
              <a-input v-model="model.chipNo" :placeholder="$t('请输入卡号')"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('姓名')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="memberName">
              <a-input v-model="model.memberName" :placeholder="$t('请输入姓名')"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('来源')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="source">
              <j-dict-select-tag type="list" v-model="model.source" dictCode="bis_member_limit_coin.source" placeholder="请选择来源" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'BisMemberLimitCoinForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           goodsName: [
              { required: true, message: this.$t('请输入名称')},
           ],
           totalCoin: [
              { required: true, message: this.$t('请输入总币值')},
           ],
           remainCoin: [
              { required: true, message: this.$t('请输入剩余币值')},
           ],
           state: [
              { required: true, message: this.$t('请输入状态')},
           ],
           source: [
              { required: true, message: this.$t('请输入来源')},
           ],
        },
        url: {
          add: "/member/bisMemberLimitCoin/add",
          edit: "/member/bisMemberLimitCoin/edit",
          queryById: "/member/bisMemberLimitCoin/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>